﻿app.controller('menuController', ['$scope', 'menuService', '$ocLazyLoad', 'tabService' ,'dicSet',function ($scope, menuService, $ocLazyLoad, tabService,dicSet) {
    $scope.menus = menuService.menus;

    $scope.click = function (elem) {
        //左边选中的菜单
        var selectMenu = menuService.selectMenuByID(elem.menu.id);
        var templateurlstr = menuService.GetTemplateUrlString(selectMenu.key);

        if (tabService.tabs.length > 0) {
            var parenttab;
            if (selectMenu.parent) {
                parenttab = tabService.getTabByMenuID(selectMenu.parent.id);
            }

            var selectTab = tabService.selectTabByMenuID(selectMenu.id);

            if (selectTab == null) {
                var item1 = new tabService.tabItem(selectMenu.key, selectMenu.value, true, selectMenu.isclose, selectMenu.id, parenttab, true, templateurlstr);
                tabService.addTab(item1);
            }
        } else if (selectMenu.ishide == false) {
            var item1 = new tabService.tabItem(selectMenu.key, selectMenu.value, true, selectMenu.isclose, selectMenu.id, null, true, templateurlstr);
            tabService.addTab(item1);
        }
    }
}])

//菜单服务，主要做菜单数据的获取，变更等操作。
app.controller('tabController', ['$scope', 'tabService', 'menuService', '$location', '$state', '$cookieStore', '$element', '$timeout', function ($scope, tabService, menuService, $location, $state, $cookieStore, $element, $timeout) {
    var selectMenu;

    console.log(menuService)
    for (var i = 0; i < menuService.menus.length; i++) {
        if (menuService.menus[i].isdefault && menuService.menus[i].ishide == false || menuService.menus[i].isdefaultPath == true) {
            selectMenu = menuService.menus[i];
            if (tabService.tabs.length > 0) {
                var selectTab = tabService.getTabByKey(menuService.menus[i].key)
                if (selectTab == null) {
                    var templateurlstr = menuService.GetTemplateUrlString(selectMenu.key);
                    var item1 = new tabService.tabItem(menuService.menus[i].key, menuService.menus[i].value, true, menuService.menus[i].isclose, menuService.menus[i].id, null, false, templateurlstr);
                    tabService.addTab(item1);
                }
            } else {
                var templateurlstr = menuService.GetTemplateUrlString(selectMenu.key);
                var item1 = new tabService.tabItem(menuService.menus[i].key, menuService.menus[i].value, true, menuService.menus[i].isclose, menuService.menus[i].id, null, true, templateurlstr);
                tabService.addTab(item1);
            }
        }
    }

    $scope.select = function (selectedTab) {
        console.log(selectedTab)
        var url = $location.path()
        var urlPath = tabService.GetUrlString(selectedTab.key, true);

        if (selectedTab) {
            selectMenu = menuService.selectMenuByID(selectedTab.menuid);
        }

        if (url != urlPath) {
            $state.go(selectedTab.key, selectedTab.params);
        }
    }

    $scope.close = function (selectedTab) {
        //var index = tabService.tabs.indexOf(selectedTab);
        //tabService.removeTab(index);
    }
    if ($cookieStore.get("zdxtToken") == null) {
        $state.go("access.signin");
        return;
    }
    tabService.selectTabByID(1);
    $scope.tabs = tabService.tabs;
}]);

app.controller('DatepickerCtrl', ['$scope', function ($scope) {
    $scope.initDate = new Date('2016-10-20');
    $scope.formats = ['dd-MMMM-yyyy', 'yyyy-MM-dd', 'yyyy/MM/dd', 'dd.MM.yyyy', 'shortDate'];
    $scope.format = $scope.formats[1];

    $scope.today = function () {
        var d = new Date();
        var year = d.getFullYear();
        var month = d.getMonth() + 1;
        if (month < 10) {
            month = '0' + month;
        }
        var date = d.getDate();
        if (date < 10) {
            date = '0' + date;
        }
        switch ($scope.format) {
            case 'yyyy-MM-dd':
                $scope.dt = year + '-' + month + '-' + date;
                break;
            case 'yyyy/MM/dd':
                $scope.dt = year + '/' + month + '/' + date;
                break;
            case 'dd.MM.yyyy':
                $scope.dt = date + '.' + month + '.' + year;
                break;
            default:
                $scope.dt = new Date();
                break;
        }
    };
    $scope.today();

    $scope.clear = function () {
        $scope.dt = null;
    };

    // Disable weekend selection
    $scope.disabled = function (date, mode) {
        return (mode === 'day' && (date.getDay() === 0 || date.getDay() === 6));
    };

    $scope.toggleMin = function () {
        $scope.minDate = $scope.minDate ? null : new Date();
    };
    $scope.toggleMin();

    $scope.open = function ($event) {
        $event.preventDefault();
        $event.stopPropagation();

        $scope.opened = true;
    };

    $scope.dateOptions = {
        formatYear: 'yyyy',
        startingDay: 1,
        class: 'datepicker'
    };
}]);
app.controller('TimepickerCtrl', ['$scope', function ($scope) {
    $scope.mytime = new Date();

    $scope.hstep = 1;
    $scope.mstep = 15;

    $scope.options = {
        hstep: [1, 2, 3],
        mstep: [1, 5, 10, 15, 25, 30]
    };

    $scope.ismeridian = true;
    $scope.toggleMode = function () {
        $scope.ismeridian = !$scope.ismeridian;
    };

    $scope.update = function () {
        var d = new Date();
        d.setHours(14);
        d.setMinutes(0);
        $scope.mytime = d;
    };

    $scope.changed = function () {
        //console.log('Time changed to: ' + $scope.mytime);
    };

    $scope.clear = function () {
        $scope.mytime = null;
    };
}])
app.controller('ModalInstanceCtrl', function ($scope, $modalInstance, content, title, okCancel, deferred) {
    $scope.Content = content;
    $scope.Title = title;
    $scope.OkCancel = okCancel;

    $scope.ok = function () {
        $modalInstance.close('ok');
        deferred.resolve();
    };

    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
        deferred.reject();
    };
});
app.controller('TypeaheadDemoCtrl', ['$scope', '$http', function ($scope, $http) {
    $scope.selected = undefined;
    $scope.states = ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado', 'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii', 'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana', 'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota', 'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire', 'New Jersey', 'New Mexico', 'New York', 'North Dakota', 'North Carolina', 'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island', 'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont', 'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming'];
    // Any function returning a promise object can be used to load values asynchronously
    $scope.getLocation = function (val) {
        return $http.get('http://maps.googleapis.com/maps/api/geocode/json', {
            params: {
                address: val,
                sensor: false
            }
        }).then(function (res) {
            var addresses = [];
            angular.forEach(res.data.results, function (item) {
                addresses.push(item.formatted_address);
            });
            return addresses;
        });
    };
}]);



